// pages/hotel/hotelDetail/book/addOccupant/addOccupant.js
let user = [];
Page({
  data: {
    array: ['身份证', '护照'],
    selected: false,
    objectArray: [
      {
        id: 0,
        name: '身份证'
      },
      {
        id: 1,
        name: '护照'
      }
    ],
    index: 0,
    idCard: '',
    phone: '',
    realName: '',
    cardType: '',
    guests: [],
    userId: 0,
  },
  getUserId() {
    var loginfo = getApp().globalData.loginfo;
    console.log(loginfo)
    this.setData({
      userId: loginfo.userId
    })
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value[0],
      cardType: this.data.array[e.detail.value[0]], // 设置证件类型
      selected: true,
    });
  },
  toSave(e) {
    console.log(e);
    const newOccupant = {  
      occupantsName: e.detail.value.occupantsName,  
      identificationNumber: e.detail.value.identificationNumber,
      occupantsPhone: e.detail.value.occupantsPhone,
      userId: this.data.userId
    };
    wx.request({
      url: 'http://localhost:8080/hotel/occupants',
      method:'POST',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data:newOccupant,
      success: (resp) => {
        console.log(resp)
        this.setData({
          // guests: resp.data.rows
        })
        wx.navigateBack()
      }
    })
    // const { idCard, phone } = this.data;
    // if (!this.data.cardType) {
    //   wx.showToast({
    //     title: '请选择证件类型',
    //     icon: 'none',
    //   });
    //   return;
    // }
    // // console.log("idCard:", this.data.idCard, "phone:", this.data.phone);

    // const isValidIdCard = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(this.data.idCard);
    // const isValidPhone = /^1[3-9]\d{9}$/.test(this.data.phone);

    // if (isValidIdCard && isValidPhone) {
    //   const newGuest = { id: Date.now(), name: this.data.realName, cardType: this.data.cardType, idCard, phone };
    //   const guests = this.data.guests;
    //   guests.push(newGuest);
    //   this.setData({ guests });
    //   wx.setStorageSync('guests', guests);
    //   wx.showToast({
    //     title: '保存成功',
    //     icon: 'success',
    //   });

    //   // 跳转到选择入住人页面
    //   wx.navigateTo({
    //     url: '/pages/hotel/hotelDetail/book/occupantSelect/occupantSelect',
    //   });
    // } else {
    //   wx.showToast({
    //     title: '请输入正确的身份证号和手机号',
    //     icon: 'none',
    //   });
    // }
    // console.log(`证件类型: ${this.data.cardType}`); 
      
  },
  handleNameInput(e) {
    this.setData({ realName: e.detail.value });
  },
  handleIdCardInput(e) {
    // console.log(111)
    this.setData({ idCard: e.detail.value });

    if (!/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(this.data.idCard)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none',
      });
    }
  },
  handlePhoneInput(e) {
    // console.log(222)
    this.setData({ phone: e.detail.value });

    if (!/^1[3-9]\d{9}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserId()
    // this.toSave(e);
    // const storedGuests = wx.getStorageSync('guests') || [];
    // this.setData({
    //   guests: storedGuests
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})